function vyberVeduceho(){
    v = $("#manazer").val();
    if (v==1){
        $("#manazer_oddelenia").show();
    }
    else {
        $("#manazer_oddelenia").hide();
    }
}

function submitForm(id){
    $("#"+id).submit();
}

function zistiManager(zid){
    ret = 0;
    $.ajax({
        url: "ajax/zistitManagerAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid,
        success: function(msg) {
            if(msg==1)
                ret = 1;
        }
    });
    return ret;
}

function pridajDochadzku(zid,date,value){
    manager = zistiManager(zid);
    $.ajax({
        url: "ajax/pridatDochadzkuAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date + "&value=" + value,
        success: function(msg) {
            if(value=='P'){
                $("#"+zid+date+"1").hide();
                $("#"+zid+date+"2").hide();
                $("#"+zid+date+"3").hide();
                $("#"+zid+date+"4").hide();
                $("#"+zid+date+"5").hide();
                $("#"+zid+date+"6").hide();
                $("#"+zid+date+"1").show();
                $("#"+zid+date+"2").show();
                $("#"+zid+date+"3").show();
                $("#"+zid+date+"4").show();
                $("#"+zid+date+"5").val('');
                $("#"+zid+date+"6").val('');
            } else if(value=='SPL'){
                $("#"+zid+date+"1").hide();
                $("#"+zid+date+"2").hide();
                $("#"+zid+date+"3").hide();
                $("#"+zid+date+"4").hide();
                $("#"+zid+date+"5").hide();
                $("#"+zid+date+"6").hide();
                $("#"+zid+date+"1").show();
                $("#"+zid+date+"2").show();
                $("#"+zid+date+"3").show();
                $("#"+zid+date+"4").show();
                $("#"+zid+date+"5").show();
                $("#"+zid+date+"6").show();
            } else {
                $("#"+zid+date+"1").hide();
                $("#"+zid+date+"2").hide();
                $("#"+zid+date+"3").hide();
                $("#"+zid+date+"4").hide();
                $("#"+zid+date+"5").hide();
                $("#"+zid+date+"6").hide();
                $("#"+zid+date+"1").val('');
                $("#"+zid+date+"2").val('');
                $("#"+zid+date+"3").val('');
                $("#"+zid+date+"4").val('');
                $("#"+zid+date+"5").val('');
                $("#"+zid+date+"6").val('');
            }
            if (manager==1){
                vypocitajMod(zid, date);
                vypocitajStravu(zid, date);
            } else {
                //vypocitajNadcas(zid, date);
                vypocitajSviatok(zid, date);
                vypocitajStravu(zid, date);
                vypocitajNocne(zid, date);
            }
            //window.location.reload(true);
            
            $(".day"+zid+date).css("background", "url('images/"+value+".png') transparent");
        }
    });
}

function nastavTerminy(zid,date,term,value){
    $('#nadcasButton').attr('disabled', 'disabled');
    manager = zistiManager(zid);
    if (value.indexOf(':')==-1){
        alert('Nesprávny formát času! Správny formát je napr. 08:00.');
        str = zid.toString();
        str+=date.toString();
        str+=term.toString();
        $("#"+str).val('');
    } else {
        $.ajax({
            url: "ajax/nastavitTerminyAjax.php",
            async: false,
            type: "POST",
            data: "zid=" + zid + "&date=" + date + "&value=" + value + "&term=" + term,
            success: function(msg) {
                if (msg==1){
                    if (manager==1){
                        vypocitajMod(zid, date);
                        vypocitajStravu(zid, date);
                    } else {
                        //vypocitajNadcas(zid, date);
                        vypocitajSviatok(zid, date);
                        vypocitajStravu(zid, date);
                        vypocitajNocne(zid, date);
                    }
                }
            }
        });
    }
    $('#nadcasButton').removeAttr('disabled');
}

function mesiacArok(date){
    ret = '';
    $.ajax({
        url: "ajax/mesiacRokAjax.php",
        async: false,
        type: "POST",
        data: "date=" + date,
        success: function(msg) {
            ret = msg;
        }
    });
    return ret;
}

function vypocitajNadcas(zid,date){
    //alert("nadcas");
    $.ajax({
        url: "ajax/vypocitatNadcasAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date,
        success: function(msg) {
            //$("#nadcas"+zid).empty();
            $("#nadcas"+zid).append(msg);
            $(".mesiacRok").empty();
            $(".mesiacRok").append(mesiacArok(date));
        }
    });
}

function vypocitajSviatok(zid,date){
    //alert("nadcas");
    $.ajax({
        url: "ajax/vypocitatSviatokAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date,
        success: function(msg) {
            //alert(msg);
            $("#sviatok"+zid).empty();
            $("#sviatok"+zid).append(msg);
            $(".mesiacRok").empty();
            $(".mesiacRok").append(mesiacArok(date));
        }
    });
}

function vypocitajStravu(zid,date){
    //alert("nadcas");
    $.ajax({
        url: "ajax/vypocitatStravuAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date,
        success: function(msg) {
            $("#strava"+zid).empty();
            $("#strava"+zid).append(msg);
            $(".mesiacRok").empty();
            $(".mesiacRok").append(mesiacArok(date));
        }
    });
}

function vypocitajNocne(zid,date){
    //alert("nadcas");
    $.ajax({
        url: "ajax/vypocitatNocneAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date,
        success: function(msg) {
            //alert(msg);
            $("#nocna"+zid).empty();
            $("#nocna"+zid).append(msg);
            $(".mesiacRok").empty();
            $(".mesiacRok").append(mesiacArok(date));
        }
    });
}

function vypocitajMod(zid,date){
    //alert("nadcas");
    $.ajax({
        url: "ajax/vypocitatModAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&date=" + date,
        success: function(msg) {
            $("#mod"+zid).empty();
            $("#mod"+zid).append(msg);
            $(".mesiacRok").empty();
            $(".mesiacRok").append(mesiacArok(date));
        }
    });
}

function copy(date,zid,userGroup){
    manager = zistiManager(zid);
    moze = confirm('Skopírovať všetky plánované do reálnych (prepíše aktuálne zadané reálne)?');
    if (moze){
        $.ajax({
            url: "ajax/copyAjax.php",
            async: false,
            type: "POST",
            data: "user_id=" + userGroup + "&date=" + date,
            success: function(msg) {
                pole = msg.split('#');
                for (i=1;i<(pole.length)-1;i++){
                    if (manager==1){
                        vypocitajMod(pole[i], date);
                        vypocitajStravu(pole[i], date);
                    } else {
                        //vypocitajNadcas(pole[i], date);
                        vypocitajSviatok(pole[i], date);
                        vypocitajStravu(pole[i], date);
                        vypocitajNocne(pole[i], date);
                    }
                }
                window.location.reload(true);
            }
        });
    }
}
function print(){
    $("#printForm").submit();
}

function unblock(email,newPass){
    $.ajax({
        url: "ajax/unblock.php",
        async: false,
        type: "POST",
        data: "email=" + email + "&pass=" + $("#"+newPass).val(),
        success: function(msg) {
            if (msg==1){
                window.location.reload(true);
            } else if (msg==0) {
                alert('Nezadali ste nové heslo!');
            } else if (msg==2) {
                alert('Takýto používateľ nie je evidovaný v systéme!');
                window.location.reload(true);
            }
        }
    });
}


function upravStravuVystupna(zid,mesiac,rok,kolko){
    $.ajax({
        url: "ajax/upravStravuAjax.php",
        async: false,
        type: "POST",
        data: "zid=" + zid + "&mesiac=" + mesiac + "&rok=" + rok + "&kolko=" + kolko,
        success: function(msg) {
            
        }
    });
}

function vypocitatNadcasy(oddelenie,date, tyzden){
    $('#overlay').css('display', 'block');
    
    setTimeout(function() {
        $.ajax({
            url: "ajax/vypocitatNadcasyAjax.php",
            async: false,
            type: "POST",
            data: "oddelenie=" + oddelenie + "&date=" + date,
            success: function(msg) {
                //alert(msg);
                if (msg!=''){
            
                }
                document.location.href = 'home.php?akc=4&tyzden='+tyzden;
            }
        });
    }, 200);
    
/**/
}

function zvyrazni(id,smer){
    if (smer==1)
        $("#"+id).css("background", "#F9FCD3");
    else
        $("#"+id).css("background", "white");
}

function exportXML (obdobie,firma)  {
    //alert(obdobie);
    //alert(firma);
    $.ajax({
        url: "ajax/xmlGenerator.php",
        async: false,
        type: "POST",
        data: "company=" + firma + "&date=" + obdobie,
        success: function(msg) {
            pole = msg.split('#');
            if (pole[0]!=1){
                alert(msg);
            } else {
                $("#vypisXml").empty();
                $("#vypisXml").append(pole[1]);
            }
        }
    });
}

function upravPoradie(poradie,userId){
    //alert(poradie);
    //alert(userId);
    $.ajax({
        url: "ajax/upravitPoradieAjax.php",
        async: false,
        type: "POST",
        data: "poradie=" + poradie + "&userId=" + userId,
        success: function(msg) {
        //alert(msg);
        }
    });
}